﻿using PRO.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PRO.Common;
using Newtonsoft.Json;
using PRO.Config;
using PRO.Entity;
using PRO.Product;
using System.Data;

namespace web.ShoppingCart
{
    public partial class view : PageBase
    {
        protected decimal TotalPrice = 0;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "购物车-" + BaseConfigs.Title;
            if (!IsPostBack)
            {
                Bind();

            }
        }
        private void Bind()
        {
            DataTable dt = ShopCart.GetCartItems(out TotalPrice);
            this.Repeater1.DataSource = dt;
            this.Repeater1.DataBind();

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string json = Utils.GetCookie("ShopCart".DEC_Encode(BaseConfigs.DEC_KEY)).DEC_Decode(BaseConfigs.DEC_KEY);
            if (!json.IsNullOrWhiteSpace())
            {
                List<ShopCartCookie> list = JsonConvert.DeserializeObject<List<ShopCartCookie>>(json);
                if (list != null && list.Count > 0)
                {
                    Response.Redirect("/deal");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('没有可结算的商品！');</script>");
                    Bind();
                }
            }
            else {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('没有可结算的商品！');</script>");
                Bind();
            }
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (!e.CommandArgument.ToString().IsNullOrWhiteSpace())
            {
                string[] arg = e.CommandArgument.ToString().Split('|');
                int id = arg[0].ToInt(0);
                int counts = 0;
                if (arg.Length == 2)
                    counts = arg[1].ToInt(0);
                if (id > 0)
                {
                    switch (e.CommandName.ToLower())
                    {
                        case "del":
                            ShopCart.DelItemToCart(id);
                            Bind();
                            break;
                        case "down":
                            if (counts <= 1)
                                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('购买商品件数不能为0！');</script>");
                            else
                            {
                                ShopCart.EditItemCounts(id, counts - 1);
                            }
                            Bind();
                            break;
                        case "up":
                            ShopCart.EditItemCounts(id, counts + 1);
                            Bind();
                            break;
                    }
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('操作失败！');</script>");
                }
            }
        }
    }
}